package org.javaboy.vhr.controller.personnel;

import org.javaboy.vhr.model.LeaveApplication;
import org.javaboy.vhr.model.RespBean;
import org.javaboy.vhr.service.LeaveApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee/leave")
public class LeaveApplicationController {

    @Autowired
    private LeaveApplicationService leaveApplicationService;


    @PostMapping("/apply")
    public RespBean applyLeave(@RequestBody LeaveApplication leaveApplication) {
        int result = leaveApplicationService.addLeaveApplication(leaveApplication);
        if (result == 1) {
            return RespBean.ok("请假申请提交成功");
        }
        return RespBean.error("请假申请提交失败");
    }

    @GetMapping("/select")
    public List<LeaveApplication> selectAll() {
        return leaveApplicationService.selectAll();
    }



    @GetMapping("/list/{employeeId}")
    public List<LeaveApplication> getLeaveApplications(@PathVariable Integer employeeId) {
        return leaveApplicationService.getLeaveApplicationsByEmployeeId(employeeId);
    }

    @PutMapping("/approve")
    public RespBean approveLeave(@RequestBody LeaveApplication leaveApplication) {
        int result = leaveApplicationService.approveLeaveApplication(leaveApplication);
        if (result == 1) {
            return RespBean.ok("请假审批成功");
        }
        return RespBean.error("请假审批失败");
    }


    @PostMapping("/test/insert")
    public RespBean testInsert(@RequestBody LeaveApplication leave) {
        System.out.println("startDate = " + leave.getStartDate());
        System.out.println("endDate = " + leave.getEndDate());
        return RespBean.ok("收到！");
    }

    @PostMapping ("/file")
    public int file(@RequestBody LeaveApplication leaveApplication) {
        return leaveApplicationService.file(leaveApplication);
    }

}
